﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Globalization;

namespace WP7Contrib.View.Controls.Converters
{
    /// <summary>
    /// A value converter that formats the value to be converted using the supplied ConverterParameter.
    /// </summary>
    /// <remarks>
    /// <para>
    /// For example, to format a numeric value to two decimal places, use as follows:
    /// </para>
    /// <code>
    /// {Binding Path=Source, Converter={StaticResource StringFormatConverter}, ConverterParameter=N2}
    /// </code>
    /// <para>
    /// Alternatively, you can supply a complete formatted text string, allowing you to add additional text. The
    /// example below formats a numeric value appending a percent sign at the end:
    /// </para>
    /// <code>
    /// &lt;Binding Path="ChangePercent"
    ///             Converter="{StaticResource StringFormatter}"
    ///             ConverterParameter=" {0:N2}%"/&gt;
    /// </code> 
    /// </remarks>
    public class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string formatParameter = (string)parameter;
            if (formatParameter.Contains("{"))
            {
                return string.Format(formatParameter, value);
            }
            else
            {
                return string.Format(culture, "{0:" + formatParameter + "}", value);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
